/** 模拟企业管理接口响应数据 */
import type { EnterpriseInfo, EnterpriseQuery } from "./type"

// 模拟企业数据列表
const MOCK_ENTERPRISE_LIST: EnterpriseInfo[] = [
  {
    id: 1,
    fullName: "北京科技有限公司",
    shortName: "北科",
    address: "北京市海淀区中关村科技园区",
    industryType: "IT互联网",
    scale: "100-500人",
    introduction: "专注于软件开发和技术服务的高科技企业",
    contactName: "张三",
    contactPhone: "13800138001",
    contactEmail: "zhangsan@example.com", // 联系邮箱
    licenseUrl: "https://example.com/license1.jpg",
    status: true,
    createTime: "2024-01-01 10:00:00"
  },
  {
    id: 2,
    fullName: "上海创新科技有限公司",
    shortName: "上创科",
    address: "上海市浦东新区张江高科技园区",
    industryType: "人工智能",
    scale: "500-1000人",
    introduction: "致力于人工智能技术研发和应用的创新企业",
    contactName: "李四",
    contactPhone: "13900139002",
    contactEmail: "lisi@example.com", // 联系邮箱
    licenseUrl: "https://example.com/license2.jpg",
    status: true,
    createTime: "2024-01-05 14:30:00"
  },
  {
    id: 3,
    fullName: "广州数字技术有限公司",
    shortName: "广数技",
    address: "广州市天河区珠江新城",
    industryType: "数字媒体",
    scale: "50-100人",
    introduction: "专注于数字媒体和创意设计的技术公司",
    contactName: "王五",
    contactPhone: "13700137003",
    contactEmail: "wangwu@example.com", // 联系邮箱
    licenseUrl: "https://example.com/license3.jpg",
    status: false,
    createTime: "2024-01-10 09:15:00"
  }
]

/** 模拟获取企业列表接口 */
export function getEnterpriseListApiMock(params: EnterpriseQuery) {
  // 模拟延迟
  return new Promise<{ code: number, data: { list: EnterpriseInfo[], total: number }, message: string }>((resolve) => {
    setTimeout(() => {
      // 模拟分页和搜索逻辑
      let filteredList = [...MOCK_ENTERPRISE_LIST]

      // 搜索过滤
      if (params.fullName) {
        filteredList = filteredList.filter(item =>
          item.fullName.includes(params.fullName!)
          || item.shortName.includes(params.fullName!)
        )
      }

      if (params.status !== undefined) {
        filteredList = filteredList.filter(item => item.status === params.status)
      }

      if (params.industryType) {
        filteredList = filteredList.filter(item => item.industryType.includes(params.industryType!))
      }

      // 分页
      const total = filteredList.length
      const start = (params.currentPage - 1) * params.size
      const end = start + params.size
      const paginatedList = filteredList.slice(start, end)

      resolve({
        code: 0,
        data: {
          list: paginatedList,
          total
        },
        message: "获取成功"
      })
    }, 500)
  })
}

/** 模拟创建企业接口 */
export function createEnterpriseApiMock(data: Omit<EnterpriseInfo, "id" | "createTime">) {
  return new Promise<{ code: number, message: string }>((resolve) => {
    setTimeout(() => {
      const newEnterprise: EnterpriseInfo = {
        ...data,
        id: Date.now(), // 模拟生成ID
        createTime: new Date().toLocaleString("zh-CN")
      }
      MOCK_ENTERPRISE_LIST.unshift(newEnterprise)
      resolve({ code: 0, message: "创建成功" })
    }, 500)
  })
}

/** 模拟更新企业接口 */
export function updateEnterpriseApiMock(data: EnterpriseInfo) {
  return new Promise<{ code: number, message: string }>((resolve) => {
    setTimeout(() => {
      const index = MOCK_ENTERPRISE_LIST.findIndex(item => item.id === data.id)
      if (index !== -1) {
        MOCK_ENTERPRISE_LIST[index] = { ...data }
        resolve({ code: 0, message: "更新成功" })
      } else {
        resolve({ code: 404, message: "企业不存在" })
      }
    }, 500)
  })
}

/** 模拟更新企业状态接口 */
export function updateEnterpriseStatusApiMock(id: number, status: boolean) {
  return new Promise<{ code: number, message: string }>((resolve) => {
    setTimeout(() => {
      const enterprise = MOCK_ENTERPRISE_LIST.find(item => item.id === id)
      if (enterprise) {
        enterprise.status = status
        resolve({ code: 0, message: "状态更新成功" })
      } else {
        resolve({ code: 404, message: "企业不存在" })
      }
    }, 500)
  })
}

/** 模拟检查企业名称是否重复接口 */
export function checkEnterpriseNameApiMock(fullName: string, id?: number) {
  return new Promise<{ code: number, data: { exist: boolean }, message: string }>((resolve) => {
    setTimeout(() => {
      const exist = MOCK_ENTERPRISE_LIST.some(item =>
        item.fullName === fullName && item.id !== id
      )
      resolve({
        code: 0,
        data: { exist },
        message: "检查成功"
      })
    }, 300)
  })
}

/** 模拟上传营业执照接口 */
export function uploadLicenseApiMock(file: File) {
  return new Promise<{ code: number, data: { url: string }, message: string }>((resolve) => {
    setTimeout(() => {
      // 模拟返回上传后的文件URL
      const url = `https://example.com/uploaded/${Date.now()}_${file.name}`
      resolve({
        code: 0,
        data: { url },
        message: "上传成功"
      })
    }, 1000)
  })
}

/** 模拟获取可用企业列表接口 */
export function getAvailableEnterpriseListApiMock() {
  return new Promise<{ code: number, data: Array<{ id: number, name: string }>, message: string }>((resolve) => {
    setTimeout(() => {
      const availableList = MOCK_ENTERPRISE_LIST
        .filter(item => item.status && item.id !== undefined)
        .map(item => ({ id: item.id as number, name: item.fullName })) // 使用类型断言确保id为number

      resolve({
        code: 0,
        data: availableList,
        message: "获取成功"
      })
    }, 500)
  })
}
